﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 角色的基类
/// </summary>
public class CharacterBase : MonoBehaviour {

    /// <summary>
    /// 当前角色的类型
    /// </summary>
    public CharacterType characterType;
    private Transform creatPoint;
    public Transform CreatePoint
    {
        get {
            if (creatPoint == null)
            {
                creatPoint = transform.Find("CreatePoint");                
            }
            return creatPoint;
        }
    } 
    private List<Card> cardList = new List<Card>();
    public List<Card> CardList { get { return cardList; } set { cardList = value; } }

    /// <summary>
    /// 是否还有手牌
    /// </summary>
    /// <returns></returns>
    public bool HasCard
    {
        get{ return (cardList.Count != 0); }
    }
    /// <summary>
    /// 根据索引获取卡牌
    /// </summary>
    /// <param name="index">索引</param>
    /// <returns></returns>
    public Card this[int index]
    {
        get { return cardList[index]; }
    }
    /// <summary>
    /// 根据卡牌得到索引
    /// </summary>
    /// <param name="card">卡牌</param>
    /// <returns></returns>
    public int this[Card card]
    {
        get { return cardList.IndexOf(card); }
    }
    /// <summary>
    /// 获取卡牌的数量
    /// </summary>
    public int CardCount
    {
        get { return cardList.Count; }
    }
    /// <summary>
    /// 排序
    /// </summary>
    public virtual void Sort(bool asc)
    {
        Tools.Sort(cardList, asc);
        SortCardUI(CardList);
    }
    /// <summary>
    /// 排序卡牌的UI
    /// </summary>
    /// <param name="cards">手牌的有序序列</param>
    public void SortCardUI(List<Card> cards)
    {
        CardUI[] cardUIs = CreatePoint.GetComponentsInChildren<CardUI>();
        for (int i = 0; i < cards.Count; i++)
        {
            for (int j = 0; j < cardUIs.Length; j++)
            {
                if (cardUIs[j]._Card == cards[i])
                {
                    cardUIs[j].SetPosition(CreatePoint, i);
                }
            }
        }
    }
    /// <summary>
    /// 添加牌
    /// </summary>
    public virtual void AddCard(Card card,bool isSeleced)
    {
        cardList.Add(card);
        //将牌设置为属于当前角色
        card.BelongTo = characterType;
        //更新显示
        CreateCardUI(card, CardCount - 1, isSeleced);
        
    }
    /// <summary>
    /// 发牌
    /// </summary>
    public virtual Card DealCard()
    {
        Card card = CardList[CardCount - 1];
        cardList.Remove(card);
        return card;
    }
    /// <summary>
    /// 创建卡牌并设置UI
    /// </summary>
    /// <param name="card">要创建的卡牌</param>
    /// <param name="index">在哪个位置</param>
    public void CreateCardUI(Card card, int index, bool isSelected)
    {
        GameObject go = PoolManager._Instance.GetObject("CardUI");
        CardUI cardUI = go.GetComponent<CardUI>();
        cardUI._Card = card;
        cardUI.IsSelected = isSelected;
        cardUI.SetPosition(CreatePoint, index);
    }

}
